
var photoLists = new Array();
var viewPhotoID = 0;
$(document).ready(function(){
	viewPhotoID = $("#view-photo-lists").attr("name");
	$("#view-photo-lists span").each(function(){
		photoLists.push($(this).text());
	});

	viewButton();
	viewImg();

	$("#view-photo-img-link").click(function(){return false;});

	$("#view-prev-photo").hover(function(){
		$(this).removeClass('prev-photo-default-arrow').addClass('prev-photo-selected-arrow');
	}, function(){
		$(this).removeClass('prev-photo-selected-arrow').addClass('prev-photo-default-arrow');
	});

	$("#view-next-photo").hover(function(){
		$(this).removeClass('next-photo-default-arrow').addClass('next-photo-selected-arrow');
	}, function(){
		$(this).removeClass('next-photo-selected-arrow').addClass('next-photo-default-arrow');
	});

	$("#view-prev-photo").click(function(){
		saveNowViewImgSrc(getPrevImgSrc());
		viewButton();
		viewImg();
	});
	$(document).keyup(function(e){
		if(37 == e.keyCode) {
			saveNowViewImgSrc(getPrevImgSrc());
			viewButton();
			viewImg();
		}
		if(39 == e.keyCode) {
			saveNowViewImgSrc(getNextImgSrc());
			viewButton();
			viewImg();
		}
	});

	$("#view-next-photo").click(function(){
		saveNowViewImgSrc(getNextImgSrc());
		viewButton();
		viewImg();
	});
});

/**
 * get the prev view photo uri
 *
 * @return string
 */
function getPrevImgSrc() {
	var prevImgSrc = null;
	var updated = 0;
	var nowImgSrc = getNowViewImgSrc();
	$.each(photoLists, function(i,n){
		if(!updated) {
			if(n) {
				if(n == nowImgSrc) {
					updated = 1;
				} else {
					prevImgSrc = n;
				}
			}
		}
	});
	return prevImgSrc;
}

/**
 * get the next view photo uri
 *
 * @return string
 */
function getNextImgSrc() {
	var nextImgSrc = null;
	var steps = photoLists.length;
	var nowImgSrc = getNowViewImgSrc();
	$.each(photoLists, function(i,n){
		if(steps) {
			if(n == nowImgSrc) {
				steps = 2;
			}
			if(1 == steps) {
				nextImgSrc = n;
			}
			steps--;
		}
	});
	return nextImgSrc;
}

/**
 * view photo to screen
 *
 * @return void
 */
function viewImg() {
	var nowViewImgSrc = $("#view-photo-img-link").css("backgroundImage");
	var nextViewImgSrc = getNowViewImgSrc();
	if(nowViewImgSrc != 'url(' + nextViewImgSrc + ')') {
		$("#view-photo-img-link").hide();
		//var refImg = new Image();
		//refImg.onload = function(){
			$("#view-photo-img-link").css("background", "url(" + nextViewImgSrc + ") no-repeat center center").show();
		//};
		//refImg.src = nextViewImgSrc;
	}
}

/**
 * check the prev or netx button is view or not
 *
 * @return void
 */
function viewButton() {
	var nowViewImgSrc = getNowViewImgSrc();
	var regex = /^http:\/\/upload\.momoger\.c/i;
	var firstItem = null;
	var lastItem = null;
	$.each(photoLists, function(i,n){
		if(n && regex.test(n)) {
			if(!firstItem) {
				firstItem = n;
			}
			lastItem = n;
		}
	});

	if(nowViewImgSrc == firstItem) {
		$("#view-prev-photo").hide();
	} else {
		$("#view-prev-photo").show();
	}
	if(nowViewImgSrc == lastItem) {
		$("#view-next-photo").hide();
	} else {
		$("#view-next-photo").show();
	}
}

/**
 * save the current view photo uri
 *
 * @param src photo uri
 * @return void
 */
function saveNowViewImgSrc(src) {
	if(src) {
		$("#view-photo-lists").attr("path", src);
	}
}

/**
 * get the current photo uri
 *
 * @return string
 */
function getNowViewImgSrc() {
	return $("#view-photo-lists").attr("path");
}
